<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Vat extends Model_Base {

    protected $_name = 'vat';
    protected $_primaryKey = 'vat_id';

    
    public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('vat' => 'vat'))
                ->where('vat_id = ?', $id)
                ->where("vat_status!= 2");
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchVat($where = "", $limit = "", $offset = "0", $order = 'vat_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('vat' => 'vat'))
                 ->where("vat_status!= 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $finalArray = array('' => '-- Select Vat --');
            foreach ($data->toArray() as $_area_type) {
                $finalArray[$_area_type['vat_id']] = $_area_type['vat_percent'];
            }
            return $finalArray;
        } else {
            return array('' => '-- Select Vat --');
        }
    }
    
    public function fetchAllVat($where = "", $limit = "", $offset = "0", $order = 'vat_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('vat' => 'vat'))
                 ->where("vat_status!= 2");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);

        $data = parent::fetchAll($select);
        if (!empty($data)) {
            
            foreach ($data->toArray() as $_area_type) {
                $finalArray[$_area_type['vat_id']] = $_area_type['vat_title'];
            }
            return $finalArray;
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get vat
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchLISTVat($where = "", $limit = "", $offset = "0", $order = "vat_id", $sort = "ASC") {
        $select = $this->select()
                ->from(array("vat" => "vat"))
                ->where("vat_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getVatCount($where) {
        $select = $this->select()
                ->from(array("vat" => "vat"), Array("cnt" => "count(*)"))
                ->where("vat_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    /**
     *  This function is used to delete record
     * 
     */
    public function deleteVat($vatArray) {
        if (is_array($vatArray)) {
            foreach ($vatArray as $_vat) {
                $vatData = Array();
                $vatData = $this->fetchEntryById($_vat);
                if (count($vatData) > 0) {
                    $data = array("vat_status" => "2");
                    $this->update($data, "vat_id =" . (int) $_vat);
                }
            }
        } else {
            $vat_id = $vatArray;
            $vatData = $this->fetchEntryById($vat_id);
            if (count($vatData) > 0) {
                $data = array("vat_status" => "2");
                $this->update($data, "vat_id =" . $vat_id);
            }
        }
    }

}
